﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Linq;
using System.Net;

public partial class PopPostID : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		
    }
    
    protected void GetPostID(string keyword)
    {
		if( string.IsNullOrEmpty(keyword) )
		{
			lblError.Visible = true;
			lblError.Text = "검색어를 입력해 주세요.";
			return;
		}
		
		string query = "http://biz.epost.go.kr/KpostPortal/openapi?regkey=";
		string authkey = "ac132f7b014657d111319105942941";
		System.Text.Encoding euckr = System.Text.Encoding.GetEncoding(51949);
		query += authkey + "&target=post&query=" + HttpUtility.UrlEncode(keyword, euckr);
		
		WebClient wc = new WebClient();
		wc.Headers["accept-language"] = "ko";

		XmlDocument xd = new XmlDocument();
		xd.Load(wc.OpenRead(query));
	
		XmlDataDocument xdd = new XmlDataDocument();
		xdd.DataSet.ReadXml(wc.OpenRead(query));
		
		if( xdd.DataSet.Tables.Count == 1 )
		{
			lblError.Visible = true;
			lblError.Text = "일치하는 결과가 없습니다.";
			return;
		}
		
		dList.DataSource = xdd.DataSet.Tables[1];
		dList.DataBind();
		
		return;
    }
    
    protected string PostIdFormat(string postId , string mode)
    {
		if( mode == "show" )
			return postId.Substring(0,3) + "-" + postId.Substring(3,3);
		else if( mode == "split")
			return postId.Substring(0,3) + "|" + postId.Substring(3,3);
		else
			return "error";
    }
    
	protected void btnSubmit_Clicked(object sender, EventArgs e)
	{
		GetPostID(Request.Form.Get("txtSearchword").Trim());
	}
}